﻿using UnityEngine;
using System.Collections;
using Memo.Electricity.Engine;


namespace Memo.Electricity.Game
{
	public class CDestination : MemoBehaviour
	{

		private int cot;

		private void OnTriggerEnter2D(Collider2D collision)
		{
			if (collision.CompareTag("Player"))
			{
				cot++;
				if (cot == 2)
				{
					//PlayerManager.Inst.RequestWin();
					StartCoroutine(WinCO());
				}

			}

		}

		private void OnTriggerExit2D(Collider2D collision)
		{
			if (collision.CompareTag("Player"))
				cot--;
		}

		private IEnumerator WinCO()
		{
			yield return null;

			Debug.Log("Win");

			GameObject p1 = PlayerManager.Inst.Player1;
			GameObject p2 = PlayerManager.Inst.Player2;

			p1.GetComponent<PlayerController>().enabled = false;
			p1.GetComponent<CharcterControlTest>().enabled = false;
			p2.GetComponent<PlayerController>().enabled = false;
			p2.GetComponent<CharcterControlTest>().enabled = false;

			CLevelMgr.Inst.GoToNextLevel();

			while (true)
			{
				//p1.transform.position = Vector3.Lerp(p1.transform.position, transform.position, 2 * Time.deltaTime);
				//p2.transform.position = Vector3.Lerp(p2.transform.position, transform.position, 2 * Time.deltaTime);
				p1.transform.position = Vector3.Lerp(p1.transform.position, transform.position, (float)(1 - System.Math.Pow(0.1f, 3.0f * Time.deltaTime)));
				p2.transform.position = Vector3.Lerp(p2.transform.position, transform.position, (float)(1 - System.Math.Pow(0.1f, 3.0f * Time.deltaTime)));
				
				yield return null;
			}

		}

	}
}